<template>
  <label class="wd-radio" :class="{ischeck: label === model}">
    <span class="wd-radio-input">
      <span class="wd-radio-inner"></span>
      <input type="radio" class="wd-radio-inp" :name="name" :value="label" v-model="model">
    </span>
    <span class="wd-radio-text">
      <slot></slot>
      <!-- 插槽没内容就渲染下面的template -->
      <!-- $slots.default表示选中所有没有具名的插槽 -->
      <template v-if="!$slots.default">{{label}}</template>
    </span>
  </label>
</template>

<script>

export default {
  name: 'WdRadio',
  computed: {
    model: {
      get () {
        return this.isGroup ? this.RadioGroup.value : this.value
      },
      set (value) {
        this.isGroup ? this.RadioGroup.$emit('input', value) : this.$emit('input', value)
      }
    },
    // 判断是否有radiogroup
    isGroup () {
      return !!this.RadioGroup
    }
  },
  props: {
    label: {
      type: [String, Number, Boolean],
      default: ''
    },
    value: null,
    name: {
      type: String,
      default: ''
    }
  },
  inject: {
    RadioGroup: {
      default: ''
    }
  }
}
</script>
<style scoped lang='scss'>
  .wd-radio {
    color: #606266;
    font-weight: 500;
    line-height: 1;
    position: relative;
    cursor: pointer;
    display: inline-block;
    white-space: nowrap;
    outline: none;
    font-size: 14px;
    margin-right: 30px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -moz-user-select: none;
    .wd-radio-input {
      white-space: nowrap;
      cursor: pointer;
      outline: none;
      display: inline-block;
      line-height: 1;
      position: relative;
      vertical-align: middle;
      .wd-radio-inner {
        border: 1px solid #dcdfef;
        border-radius: 100%;
        width: 14px;
        height: 14px;
        background-color: #fff;
        position: relative;
        cursor: pointer;
        display: inline-block;
        box-sizing: border-box;
        &::after {
          width: 4px;
          height: 4px;
          border-radius: 100%;
          background-color: #fff;
          content: "";
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%,-50%) scale(0);
          transition: transform 0.15s ease-in;
        }
      }
    }
  }
  .wd-radio-inp {
    opacity: 0;
    outline: none;
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
  }
.wd-radio.ischeck {
  .wd-radio-input {
    .wd-radio-inner {
      background-color: #409eff;
      border-color: #409eff;
      &::after {
        transform: translate(-50%, -50%) scale(1);
      }
    }
  }
}
</style>
